package br.com.hmm.mb;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.com.hmm.dao.FabricaConexao;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class RelatorioBean {
	
	public RelatorioBean(){
		
	}
	
    private String getDiretorioReal(String diretorio) { 
		  HttpSession session = (HttpSession) 
		FacesContext.getCurrentInstance().getExternalContext().getSession(false); 
		  return session.getServletContext().getRealPath(diretorio); 
		} 
	
	public void gerarRelDespesa() throws JRException, SQLException, IOException {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest req = (HttpServletRequest) context
				.getExternalContext().getRequest();
//
//		Integer cod = new Integer(req.getParameter("codigoEntrada")).intValue();
//
		Map parameters = new HashMap();
//		parameters.put("cod", new Integer(cod));
		FabricaConexao.getInstancia();
		String rel = getDiretorioReal("rel/RelatorioDespesas.jasper");
		JasperPrint print = JasperFillManager.fillReport(rel,parameters,FabricaConexao.conectar());
		byte[] bytes = JasperExportManager.exportReportToPdf(print);

		HttpServletResponse response = (HttpServletResponse) context
				.getExternalContext().getResponse();
		/***********************************************************************
		 * Pour afficher une bo�te de dialogue pour enregistrer le fichier sous
		 * le nom rapport.pdf
		 **********************************************************************/
		response.addHeader("Content-disposition",
				"attachment;filename=RelatorioDespesas.pdf");
		response.setContentLength(bytes.length);
		response.getOutputStream().write(bytes);
		response.setContentType("application/pdf");
		context.responseComplete();

	}
	
	public void gerarRelEntrada() throws JRException, SQLException, IOException {
		FacesContext context = FacesContext.getCurrentInstance();
		HttpServletRequest req = (HttpServletRequest) context
				.getExternalContext().getRequest();
//
//		Integer cod = new Integer(req.getParameter("codigoEntrada")).intValue();
//
		Map parameters = new HashMap();
//		parameters.put("cod", new Integer(cod));
		FabricaConexao.getInstancia();
		String rel = getDiretorioReal("rel/RelatorioEntradas.jasper");
		JasperPrint print = JasperFillManager.fillReport(rel,parameters,FabricaConexao.conectar());
		byte[] bytes = JasperExportManager.exportReportToPdf(print);

		HttpServletResponse response = (HttpServletResponse) context
				.getExternalContext().getResponse();
		/***********************************************************************
		 * Pour afficher une bo�te de dialogue pour enregistrer le fichier sous
		 * le nom rapport.pdf
		 **********************************************************************/
		response.addHeader("Content-disposition",
				"attachment;filename=RelatorioDespesas.pdf");
		response.setContentLength(bytes.length);
		response.getOutputStream().write(bytes);
		response.setContentType("application/pdf");
		context.responseComplete();

	}


}
